﻿using WQLWebAPI.Domain.Abstract;
using WQLWebAPI.Domain.Entities;
using WQLWebAPI.Infrastructure.CommonDtos;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace WQLWebAPI.Application.Loggers
{
    public class LogService : ILogService
    {
        private IBaseRepository<ErrorLog> _errorLogRepository;
        private IBaseRepository<InfoLog> _infoLogRepository;
        public LogService(IBaseRepository<ErrorLog> errorLogRepository, IBaseRepository<InfoLog> infoLogRepository)
        {
            _errorLogRepository = errorLogRepository;
            _infoLogRepository = infoLogRepository;
        }
        public async Task<Pagelist<ErrorLog>> GetErrorLogerAsync(PageInputDto pageInput)
        {
            var result = await _errorLogRepository.GetAsync(null, pageInput);
            return result;
        }
        public List<ErrorLog> GetErrorLoger(PageInputDto pageInput)
        {
            var result = _errorLogRepository.Get(null, pageInput);
            return result;
        }

        public async Task<Pagelist<InfoLog>> GetInfoLogerAsync(PageInputDto pageInput)
        {
            var builder = Builders<InfoLog>.Filter;
            var filterStr = pageInput.Search;
            var result = new Pagelist<InfoLog>();
            if (!string.IsNullOrEmpty(filterStr))
            {
                var filter = builder.Where(d => d.Properties["Operation"].Contains(filterStr) || d.Properties["Username"].Contains(filterStr));
                result = await _infoLogRepository.GetAsync(filter, pageInput);

            }
            else
            {
                result = await _infoLogRepository.GetAsync(null, pageInput);
            }

            return result;
        }
    }
}
